package com.jt9003.controller;


import com.jt9003.pojo.Drugs;
import com.jt9003.service.ItemsService;
import com.jt9003.utils.PageUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/items")
public class ItemsController {
    @Resource
    private ItemsService itemsService;

    //添加收费类型√
    @RequestMapping("/insertItem")
    public Map<String, Object> insertItem(Drugs drugs) {
        Map<String, Object> map = new HashMap();
        System.out.println("drugs="+drugs.getDrName()+"--"+drugs.getDrUsage()+"--"+drugs.getDrType());
        int i = itemsService.insertItem(drugs);
        if (i > 0) {
            map.put("msg", true);
            map.put("data", drugs);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteItemById")
    public Map<String, Object> deleteItemById(int drId) {
        Map<String, Object> map = new HashMap();
        int i = itemsService.deleteItemById(drId);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateItemById")
    public Map<String, Object> updateItemById(Drugs drugs) {
        Map<String, Object> map = new HashMap();
        int i = itemsService.updateItemById(drugs);
        System.out.println(drugs.getDrId() + "---" + drugs.getDrUsage());
        if (i > 0) {
            map.put("msg", true);
            map.put("data", drugs);
        } else {
            map.put("msg", false);
        }
        System.out.println("map="+map);
        return map;
    }

    @RequestMapping("/updateItemStateById")
    public Map<String, Object> updateItemStateById(Drugs drugs) {
        Map<String, Object> map = new HashMap();
        int i = itemsService.updateItemStateById(drugs);
        System.out.println(i);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", drugs);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectDrugsByMultipleConditions")
    public Map<String, Object> selectDrugsByMultipleConditions(Integer drRecuperationType, Integer drType, Integer drState, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                               @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = itemsService.getDrugsCountByMultipleConditions(drRecuperationType,drType,drState);
        List<Drugs> list = itemsService.selectDrugsByMultipleConditions(drRecuperationType, drType, drState, start, limit);
        System.out.println("============"+list);
        if (list!=null) {
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getDrugsCountByMultipleConditions")
    public Map<String, Object> getDrugsCountByMultipleConditions(Integer drRecuperationType, Integer drType, Integer drState) {
        Map<String, Object> map = new HashMap();
        int i = itemsService.getDrugsCountByMultipleConditions(drRecuperationType, drType, drState);
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectDrugsByNameOrPinyinInitials")
    public Map<String, Object> selectDrugsByNameOrPinyinInitials(String drName,String drCode, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                 @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = itemsService.getDrugsCountByResult(drName,drCode);
        List<Drugs> list = itemsService.selectDrugsByNameOrPinyinInitials(drName,drCode, start, limit);
        if (list.size()>0) {
            for(int i=0;i<list.size();i++){
                if (list.get(i).getDrType()==10){
                    list.get(i).setDrTypeShow("饮片");
                } else if (list.get(i).getDrType()==11){
                    list.get(i).setDrTypeShow("颗粒");
                } else if (list.get(i).getDrType()==12){
                    list.get(i).setDrTypeShow("袋装颗粒");
                }
                if (list.get(i).getDrRecuperationType()==17){
                    list.get(i).setDrRecuperationTypeShow("中药处方");
                } else if (list.get(i).getDrRecuperationType()==18){
                    list.get(i).setDrRecuperationTypeShow("西药处方");
                } else if (list.get(i).getDrRecuperationType()==19){
                    list.get(i).setDrRecuperationTypeShow("中成药处方");
                }
                if (list.get(i).getDrUnit()==1){
                    list.get(i).setDrUnitShow("克");
                } else if (list.get(i).getDrUnit()==2){
                    list.get(i).setDrUnitShow("千克");
                } else if (list.get(i).getDrUnit()==3){
                    list.get(i).setDrUnitShow("包");
                } else if (list.get(i).getDrUnit()==4){
                    list.get(i).setDrUnitShow("盒");
                } else if (list.get(i).getDrUnit()==5){
                    list.get(i).setDrUnitShow("袋");
                } else if (list.get(i).getDrUnit()==6){
                    list.get(i).setDrUnitShow("支");
                } else if (list.get(i).getDrUnit()==7){
                    list.get(i).setDrUnitShow("次");
                } else if (list.get(i).getDrUnit()==8){
                    list.get(i).setDrUnitShow("片");
                } else if (list.get(i).getDrUnit()==9){
                    list.get(i).setDrUnitShow("疗程");
                }
            }
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getDrugsCountByResult")
    public Map<String, Object> getDrugsCountByResult(String drName,String drCode) {
        Map<String, Object> map = new HashMap();
        int i = itemsService.getDrugsCountByResult(drName,drCode);
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectDrugsList")
    public Map<String, Object> selectDrugsList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                   @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = itemsService.getDrugsCount();
        List<Drugs> list = itemsService.selectDrugsList(start, limit);

        for(int i=0;i<list.size();i++){
            if (list.get(i).getDrType()==10){
                list.get(i).setDrTypeShow("饮片");
            } else if (list.get(i).getDrType()==11){
                list.get(i).setDrTypeShow("颗粒");
            } else if (list.get(i).getDrType()==12){
                list.get(i).setDrTypeShow("袋装颗粒");
            }
            if (list.get(i).getDrRecuperationType()==17){
                list.get(i).setDrRecuperationTypeShow("中药处方");
            } else if (list.get(i).getDrRecuperationType()==18){
                list.get(i).setDrRecuperationTypeShow("西药处方");
            } else if (list.get(i).getDrRecuperationType()==19){
                list.get(i).setDrRecuperationTypeShow("中成药处方");
            }
            if (list.get(i).getDrUnit()==1){
                list.get(i).setDrUnitShow("克");
            } else if (list.get(i).getDrUnit()==2){
                list.get(i).setDrUnitShow("千克");
            } else if (list.get(i).getDrUnit()==3){
                list.get(i).setDrUnitShow("包");
            } else if (list.get(i).getDrUnit()==4){
                list.get(i).setDrUnitShow("盒");
            } else if (list.get(i).getDrUnit()==5){
                list.get(i).setDrUnitShow("袋");
            } else if (list.get(i).getDrUnit()==6){
                list.get(i).setDrUnitShow("支");
            } else if (list.get(i).getDrUnit()==7){
                list.get(i).setDrUnitShow("次");
            } else if (list.get(i).getDrUnit()==8){
                list.get(i).setDrUnitShow("片");
            } else if (list.get(i).getDrUnit()==9){
                list.get(i).setDrUnitShow("疗程");
            }
        }
        if (list.size() > 0) {
            System.out.println("================start:" + start);
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteDrugsById")
    public Map<String, Object> deleteDrugsById(String checkList) {
        Map<String, Object> map = new HashMap();
        System.out.println("==>" + checkList);

        String[] strs = checkList.split(",");
        System.out.println(strs[0]);
        List<Integer> ids = new ArrayList<>();
        for (String str : strs) {
            ids.add(Integer.parseInt(str));

        }

        int i = itemsService.deleteDrugsById(ids);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getDrugsCount")
    public Map<String, Object> getDrugsCount() {
        Map<String, Object> map = new HashMap();
        int i = itemsService.getDrugsCount();
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }


}
